//
//  LGPatientDetailHealthCell.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/6/5.
//  患者档案 - 健康档案Cell

#import "LGPatientDetailHealthCell.h"

@interface LGPatientDetailHealthCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)NSArray *itemArray;
/**   */
@property (nonatomic , strong)NSMutableArray *viewArray;

@end

@implementation LGPatientDetailHealthCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setType:(NSString *)type{
    [super setType:type];
    CGFloat top = [type isEqualToString:@"bottom"]?viewPix(8)+15:viewPix(8);
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(top);
    }];
    CGFloat bottom = [type isEqualToString:@"bottom"]?-viewPix(15):-viewPix(10);
    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.baseView).offset(bottom);
    }];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    self.titleLabel.text = dataDic[@"title"];
    self.itemArray = dataDic[@"items"];
//    [self.collectionView reloadData];
//    [self resetCollectionHeight];
//    [self performSelector:@selector(resetCollectionHeight) withObject:nil afterDelay:0.2];
}

-(void)setItemArray:(NSArray *)itemArray{
    _itemArray = itemArray;
    for (LGPatientHealthItemView *itemView in self.viewArray) {
        [itemView removeFromSuperview];
    }
    self.viewArray = [NSMutableArray array];
    CGFloat left = 0;
    CGFloat top = 0;
    CGFloat bottom = 0;
    CGFloat margin = viewPix(10);
    CGFloat maxWidth = Screen_W-viewPix(65);
    for (NSInteger i=0; i<itemArray.count; i++) {
        NSString *content = itemArray[i];
        CGFloat contentW = [content textWidthWithFont:LGFont(14) maxHeight:MAXFLOAT] + viewPix(40);
        contentW = contentW>maxWidth?maxWidth:contentW;
        CGFloat contentH = [content textHeightWithFont:LGFont(14) maxWidth:maxWidth-viewPix(40)];
        contentH = contentH<viewPix(30)?viewPix(30):contentH;
        top = (left+contentW>maxWidth || contentH>viewPix(30))?bottom+margin:top;
        left = (left+contentW>maxWidth || contentH>viewPix(30))?0:left;
        LGPatientHealthItemView *itemView = [[LGPatientHealthItemView alloc]initWithFrame:CGRectMake(left, top, contentW, contentH)];
        itemView.content = content;
        [self.itemView addSubview:itemView];
        [self.viewArray addObject:itemView];
        bottom = kMaxY(itemView.frame);
        left += contentW+margin;
    }
    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(bottom));
    }];
}



//-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    return self.itemArray.count;
//}
//
//-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    LGPatientHealthItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    cell.content = self.itemArray[indexPath.item];
//    return cell;
//}
//
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSString *content = self.itemArray[indexPath.item];
//    CGFloat maxWidth = Screen_W-viewPix(65);
//    CGFloat contentW = [content textWidthWithFont:LGFont(14) maxHeight:MAXFLOAT] + viewPix(40);
//    contentW = contentW>maxWidth?maxWidth:contentW;
//    CGFloat contentH = [content textHeightWithFont:LGFont(14) maxWidth:maxWidth-viewPix(40)];
//    contentH = contentH<viewPix(30)?viewPix(30):contentH;
//    return CGSizeMake(contentW, contentH);
//}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.itemView];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(5));
        make.left.equalTo(self.baseView).offset(viewPix(16));
        make.right.equalTo(self.baseView).offset(-viewPix(16));
    }];
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(6));
        make.left.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.baseView).offset(-viewPix(15));
        make.height.equalTo(@(viewPix(30)));
    }];
}


-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#61616D" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _titleLabel;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc]init];
    }
    return _itemView;
}



@end




@interface LGPatientHealthItemView()

/**   */
@property (nonatomic , strong)UILabel *contentLabel;

@end

@implementation LGPatientHealthItemView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:0];
        [self.contentLabel addBorder:@"#E8E4F1" lineWidth:1 cornerRidus:viewPix(15)];
        self.contentLabel.backgroundColor = [UIColor colorWithString:@"#F6F5FA"];
        self.contentLabel.clipsToBounds = YES;
        [self addSubview:self.contentLabel];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self);
        }];
    }
    return self;
}

-(void)setContent:(NSString *)content{
    _content = content;
    self.contentLabel.text = content;
}

@end
